﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using LdtJudge.DataEngine.DataBaseStores;
using LdtJudge.DataEngine.Model.ElmentModel;
using Sunny.UI;

namespace Ldt.WindowsFormsApp
{
    public partial class ShowDynamicData : UIForm
    {
        /// <summary>
        /// 分析上下文
        /// </summary>
        DataContext dataContext = null;
        List<TreeNode> treeNodes = new List<TreeNode>();
        /// <summary>
        /// 表数据
        /// </summary>
        Dictionary<string, DataTable> dataTablePro = new Dictionary<string, DataTable>();
        /// <summary>
        /// 案由要素数据
        /// </summary>
        Dictionary<string, List<NodeModelList>> caseCauseValueParirsList = new Dictionary<string, List<NodeModelList>>();

        /// <summary>
        /// 案由要素节点字典
        /// </summary>
        Dictionary<string, NodeModelList> elementNodeModeListDic = new Dictionary<string, NodeModelList>();

        /// <summary>
        /// 人员实体字典
        /// </summary>
        Dictionary<string, string> spockDic = new Dictionary<string, string>();

        public ShowDynamicData( DataContext _dataContext)
        {
            InitializeComponent();
            dataContext = _dataContext;
        }

        private void ShowDynamicData_Load(object sender, EventArgs e)
        {
            //tablePage.Parent = null;
            //uiTabControl1.SelectedIndex = 1;
            LoadPageData();
        }

        /// <summary>
        /// tab切换事件
        /// </summary>
        private void uiTabControl1_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadPageData();
        }

        private void LoadPageData()
        {
            switch (uiTabControl1.SelectedIndex)
            {
                case 0:
                    
                case 1:
                    LoadElementComboBoxData();
                    break;
                default:
                    break;
            }
        }

        /// <summary>
        /// 加载要素数据-人员实体下拉框数据
        /// </summary>
        private void LoadElementComboBoxData()
        {
            if(dataContext!=null)
            {
                caseCauseValueParirsList = dataContext.DataLayerAns.CaseCauseElementList;
                spockDic.Clear();
                elementNodeModeListDic.Clear();
                int i = 0;
                foreach (KeyValuePair<string, List<NodeModelList>> item in caseCauseValueParirsList)
                {
                    List<NodeModelList> casecauseElementList = item.Value;
                    foreach (NodeModelList casecauseElement in casecauseElementList)
                    {
                        string spname = string.Empty;
                        if (casecauseElement.NodeModelKey.SP_Name.Length > 10)
                        {
                            spname = casecauseElement.NodeModelKey.SP_Name.Substring(0, 4) + "***" + casecauseElement.NodeModelKey.SP_Name.Substring(casecauseElement.NodeModelKey.SP_Name.Length - 2);
                        }
                        else
                        {
                            spname = casecauseElement.NodeModelKey.SP_Name;
                        }
                        string comtext = spname + "---" + casecauseElement.NodeModelKey.SP_SuitdegreeID + "---" + casecauseElement.NodeModelKey.ElementType;
                        spockDic.Add(i.ToString(), comtext);
                        elementNodeModeListDic.Add(i.ToString(), casecauseElement);
                        i += 1;
                    }
                    
                }
                BindingSource spokebs = new BindingSource();
                spokebs.DataSource = spockDic;
                elementComboBox.SelectedIndexChanged -= new EventHandler(elementComboBox_SelectedIndexChanged);
                elementComboBox.DataSource = spokebs;
                elementComboBox.ValueMember = "Key";
                elementComboBox.SelectedIndexChanged += new EventHandler(elementComboBox_SelectedIndexChanged);
                elementComboBox.DisplayMember = "Value";
            }
        }

        /// <summary>
        /// 要素数据-人员实体下拉框Change事件
        /// </summary>
        private void elementComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadELementTreeData();
        }

        /// <summary>
        /// 仅显示有提取值Changed事件
        /// </summary>
        private void isExtSwitch_ActiveChanged(object sender, EventArgs e)
        {
            LoadELementTreeData();
        }

        /// <summary>
        /// 加载案由要素树结构
        /// </summary>
        private void LoadELementTreeData()
        {
            if (elementComboBox.SelectedIndex > -1)
            {
                elementTreeView.Nodes.Clear();
                string spockValue = elementComboBox.SelectedValue.ToString();
                TreeNode topNode = new TreeNode();
                topNode.Name = dataContext.DataLayerAns.CaseCausesList[0].CC_ID;
                topNode.Text = dataContext.DataLayerAns.CaseCausesList[0].CC_Name;
                List<NodeModel> models = elementNodeModeListDic[spockValue].BeforeMergerListModels;
                string elementType = elementNodeModeListDic[spockValue].NodeModelKey.ElementType.ToString();
                BindElementTreeNode(topNode, topNode.Name, elementType, models);
                elementTreeView.Nodes.Add(topNode);
                if (dataContext.DataLayerAns.CaseType == "a1")
                {
                    var topNodetwo = new TreeNode();
                    topNodetwo.Name = "CC1806070922410001";
                    topNodetwo.Text = "刑事附带民事要素结构";

                    BindElementTreeNode(topNodetwo, topNodetwo.Name, elementType, models);
                    elementTreeView.Nodes.Add(topNodetwo);
                }
            }
        }
        
        /// <summary>
        /// 加载案由要素树子级结构
        /// </summary>
        /// <param name="rootNode"></param>
        /// <param name="guidID"></param>
        /// <param name="elementType"></param>
        /// <param name="model"></param>
        private void BindElementTreeNode(TreeNode rootNode, string guidID, string elementType, List<NodeModel> model)
        {
            List<NodeModel> models = new List<NodeModel>();
            if (isExtSwitch.Active)
            {
                models = model.Where(mo => mo.ParentGuidID == guidID && mo.IsNullExtOriginalText)
                              .Where(mo=>!mo.NodeName.EndsWith("_OriginalText"))          
                              .ToList();
            }
            else
            {
                models = model.Where(mo => mo.ParentGuidID == guidID)
                              .Where(mo => !mo.NodeName.EndsWith("_OriginalText"))
                              .ToList();
            }
            rootNode.Nodes.Clear();
            foreach (var mo in models)
            {
                var NodeTemp = new TreeNode();
                NodeTemp.Name = mo.GuidID;
                NodeTemp.Text = mo.NodeName;
                if (elementType == "证据要素" && mo.NodeName == "事实要素")
                {
                    NodeTemp.Text = mo.NodeName + "|" + mo.IsConfirm;
                }
                NodeTemp.Tag = mo.NodeID;
                rootNode.Nodes.Add(NodeTemp);
                if(model.Any(x=>x.ParentGuidID==mo.GuidID))
                {
                    BindElementTreeNode(NodeTemp, mo.GuidID, elementType, model);
                }
            }
        }

        /// <summary>
        /// 案由要素树节点选中事件
        /// </summary>
        private void elementTreeView_AfterSelect(object sender, TreeViewEventArgs e)
        {
            TreeNode item = elementTreeView.SelectedNode;
            if (item != null)
            {
                //这里判定加入一个刑事附带民事的判断
                if (item.Name == dataContext.DataLayerAns.CaseCausesList[0].CC_ID || item.Name == "CC1806070922410001")
                {
                    //这里表示是根节点，不进行触发事件。
                    return;
                }
                //选择当前节点，进行显示内容，和提取过程
                string spockValue = elementComboBox.SelectedValue.ToString();
                List<NodeModel> models = elementNodeModeListDic[spockValue].BeforeMergerListModels;
                //返回一个节点
                NodeModel node = models.FirstOrDefault(a => a.GuidID == item.Name);
                //提取前的原文值
                orgOldRichTextBox.Clear();
                orgOldRichTextBox.Text = node.OriginalText;
                orgNewRichTextBox.Clear();
                if (node.IsNullExtOriginalText)
                {
                    for (int i = 0; i < node.ExtOriginalText.Count; i++)
                    {
                        orgNewRichTextBox.AppendText("原文【集合第" + (i + 1) + "个】为：" + node.ExtOriginalText[i] + "\r\n");
                    }
                }
                //条件配置
                regexRichTextBox.Clear();
                regexRichTextBox.AppendText(LdtJudge.DataEngine.CallInterface.ConfigManager.ConfigManagerHandle.ReturnJsonString(node.ElementCommWhere));
                //参数配置
                nodeParamRichTextBox.Clear();
                if(node.ElementConfigModel!=null)
                {
                    nodeParamRichTextBox.AppendText(LdtJudge.DataEngine.CallInterface.ConfigManager.ConfigManagerHandle.ReturnJsonString(node.ElementConfigModel));
                }
                
            }
            
        }
    }
}
